package com.rayenterprise.kms.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.rayenterprise.kms.client.remote.HolderRemote;
import com.rayenterprise.kms.client.remote.NotionRemote;
import com.rayenterprise.kms.domain.KHolder;
import com.rayenterprise.kms.domain.KNotion;
import com.rayenterprise.kms.events.AfterHolderNodeChangeEvent;

public class HolderEditTab extends AbstractCustomEditTab {

	private KHolder holder = new KHolder();

	public HolderEditTab(HandlerManager eventBus) {
		super(eventBus);
		// TODO Auto-generated constructor stub
	}

	public HolderEditTab(Object obj, AbstractCustomNode node,
			HandlerManager eventBus) {
		super(obj, node, eventBus);
		holder = (KHolder) obj;
		text.setText(holder.getTitle());
	}

	@Override
	public void customObjectInit() {

		name.setText("Holder Name:");
		tab.setAnimationEnabled(true);
		tab.add(hp, "save holder");
		tab.selectTab(0);

		button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				holder.setTitle(text.getValue());
				HolderRemote.Util.getInstance().save(holder,
						new AsyncCallback<KHolder>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								name.setText(caught.getMessage());

							}

							@Override
							public void onSuccess(KHolder result) {
								if (node == null) {
									node = new HolderNode(result, eventBus);
									AfterHolderNodeChangeEvent sent = new AfterHolderNodeChangeEvent(
											node);
									eventBus.fireEvent(sent);
								} else {
									node.setLblText(result.getTitle());						
								}

							}
						});

			}
		});

		/*
		 * Add Notion Tab and its behaviors, this is far from perfection it is
		 * going to be revised in the next version
		 */
		if (node != null) {
			Label lblltNotion = new Label("Notion Name:");
			final TextBox txtNotion = new TextBox();
			HorizontalPanel hpNotion = new HorizontalPanel();
			Button btn = new Button("save");
			//btn.setStyleName("form-button");
			lblltNotion.setStyleName("form-label");
			txtNotion.setStyleName("form-text");
			hpNotion.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
			hpNotion.add(lblltNotion);
			hpNotion.add(txtNotion);
			hpNotion.add(btn);
			hpNotion.setSpacing(5);
			

			tab.add(hpNotion, "new notion");
			btn.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					KNotion notion = new KNotion();
					notion.setName(txtNotion.getValue());
					notion.setHolder(holder);
					NotionRemote.Util.getInstance().save(notion,
							new AsyncCallback<KNotion>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									Window.alert(caught.getMessage());

								}

								@Override
								public void onSuccess(KNotion notion) {
										NotionNode nn = new NotionNode(notion, eventBus);										
										node.addItem(nn);			

								}
							});

				}
			});
			//Add Delete Tab
			Label lblDelete = new Label("Delete This Holder? ");
			lblDelete.setStyleName("form-label");
			Button btnDelete = new Button("delete");
			//btnDelete.setStyleName("form-button");
			HorizontalPanel delHp = new HorizontalPanel();
			delHp.setSpacing(5);
			delHp.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
			delHp.add(lblDelete);
			delHp.add(btnDelete);
			
			tab.add(delHp,"delete Holder");
			btnDelete.addClickHandler(new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					if(Window.confirm("Realy want to delete this Holder?")){
						HolderRemote.Util.getInstance().deleteHolder(holder, new AsyncCallback<Void>(){

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onSuccess(Void result) {
								// TODO Auto-generated method stub
								Window.alert("Delete Successful!");
								AfterHolderNodeChangeEvent sent = new AfterHolderNodeChangeEvent(
										node);
								eventBus.fireEvent(sent);
								
							}});
					}
					
				}
				
			});
		}

		this.add(tab);
		this.setWidth("350px");

	}

}
